﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;

namespace WebPages.Dal
{
    public class ProfileDataDataSource
    {
        AzureServiceContext serviceContext = null;

        static CloudStorageAccount GetStorageAccount()
        {
            return CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"));
        }

        static AzureServiceContext GetServiceContext()
        {
            var storageAccount = GetStorageAccount();
            return new AzureServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials);
        }

        void InitServiceContext()
        {
            serviceContext = GetServiceContext();
        }

        static void CreateUserTableIfNotExists()
        {
            var storageAccount = GetStorageAccount();

            storageAccount.CreateCloudTableClient().CreateTableIfNotExist(AzureServiceContext.ProfileDataTableName);
        }

        public ProfileDataDataSource()
        {
            InitServiceContext();

            CreateUserTableIfNotExists();
        }

        /// <summary>
        /// Creates a composite Azure Table Storage partition key
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        string CreatePartitionKey(string applicationName, string userName, string propertyName)
        {
            return String.Format("{0}>{1}>{2}", applicationName, userName, propertyName);
        }

        #region CRUD operations

        public IEnumerable<ProfileDataEntity> GetProfileData(string applicationName, string userName, string propertyName)
        {
            var results = from c in serviceContext.ProfileData
                          where c.PartitionKey == CreatePartitionKey(applicationName, userName, propertyName)
                          select c;

            var query = results.AsTableServiceQuery<ProfileDataEntity>();
            return query.Execute();
        }


        public IEnumerable<ProfileDataEntity> SetProfileData(string applicationName, string userName, string propertyName, bool propertyValue)
        {
            var results = from c in serviceContext.ProfileData
                          where c.PartitionKey == CreatePartitionKey(applicationName, userName, propertyName)
                          select c;

            var query = results.AsTableServiceQuery<ProfileDataEntity>();
            return query.Execute();
        }

        public void Delete(ProfileDataEntity itemToDelete)
        {
            serviceContext.DeleteObject(itemToDelete);
            serviceContext.SaveChanges();
        }

        public void Insert(ProfileDataEntity newItem)
        {
            serviceContext.AddObject(AzureServiceContext.ProfileDataTableName, newItem);
            serviceContext.SaveChanges();
        }

        public void Update(ProfileDataEntity itemToUpdate)
        {
            serviceContext.UpdateObject(itemToUpdate);
            serviceContext.SaveChanges();
        }

        #endregion
    }
}